<?php

namespace app\api\service;

use app\common\model\article\Article;
use app\common\model\article\ArticleCate;
use app\common\model\diy\Diy;
use tmcore\base\BaseService;

class ArticleService extends BaseService
{
    /**
     * 获取文章列表
     * @return array
     */
    public static function getList(): array
    {
        // 搜索条件
        $searchParam = [
            '%like%' => ['title']
        ];
        $extend_where = [['status' ,'=', 1]];
        // 分类筛选
        $cate_id = request()->param('cate_id');
        if($cate_id) {
            $cateIds = ArticleCate::where(['pid' => $cate_id])->column('id');
            if($cateIds) {
                array_push($cateIds, $cate_id);
                $extend_where[] = ['cate_id', 'in', $cateIds];
            } else {
                $extend_where[] = ['cate_id', 'in', $cate_id];
            }
        }
        $where = self::getSearchData($searchParam, $extend_where);
        
        // 返回分页数据
        $list =  Article::page($where, self::getPageData(),'id,title,cate_id,image,desc,actual_hits,init_hits,sort,status,create_time', ['sort' => 'desc', 'id' => 'desc'], ['cate_name', 'hits', 'status_desc']);

        foreach ($list['list'] as &$item) {
            $item['image'] =  get_file_url($item['image']);
        }

        return $list;;
    }

    /**
     * 获取首页文章列表
     * @param $params
     * @return array
     */
    public static function getHomeList($params): array
    {
        // 搜索条件
        $where = [['status', '=', 1]];
        if(isset($params['is_recommend'])) $where[] = ['is_recommend', '=', $params['is_recommend']];

        $list = Article::where($where)->limit($params['limit']??6)->field('id,cate_id,title,desc,image,actual_hits,init_hits,is_recommend,status,sort,create_time')->order(['sort' => 'desc', 'id' => 'desc'])->append(['cate_name', 'hits', 'status_desc'])->select()->toArray();

        foreach ($list as $key => $value)  {
            $list[$key]['image'] = get_file_url($value['image']);
        }

        return $list;
    }

    /**
     * 查看文章详情
     * @param $params
     * @param bool $is_inc
     * @return array
     */
    public static function detail($params, $is_inc = false) : array
    {
        $article = Article::where(['id' => $params['id']])
            ->findOrEmpty();
        if ($article->isEmpty()) return [];

        // 点击量自增
        if($is_inc)$article->inc('actual_hits',1)->update();

        $detail = $article->append(['hits','cate_name'])->toArray();

        // 上级栏目名称
        if($detail['cate_id']) {
            $pid = ArticleCate::where(['id' => $detail['cate_id']])->value('pid');
            $detail['cate_pid'] = $pid?$pid:0;
            $detail['cate_p_name'] = $pid?ArticleCate::where(['id' => $pid])->value('name'):'';
        } else {
            $detail['cate_pid'] = 0;
            $detail['cate_p_name'] = '';
        }

        // 获取分类下的文章
        $article_list = Article::field(['id', 'cate_id', 'title'])
            ->where(['status' => 1, 'cate_id' => $detail['cate_id']])
            ->order(['sort' => 'desc', 'id' => 'desc'])
            ->select()->toArray();

        $nowIndex = 0;
        foreach ($article_list as $key => $item) {
            if ($item['id'] == $detail['id']) {
                $nowIndex = $key;
            }
        }
        // 上一篇
        $detail['last'] = $article_list[$nowIndex - 1] ?? [];
        // 下一篇
        $detail['next'] = $article_list[$nowIndex + 1] ?? [];

        return $detail;
    }

}